package com.huaxun.eduPlan.web;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.optix.common.app.AppContext;
import net.optix.common.app.ILoginAction;
import net.optix.common.app.UserLoginAction;
import net.optix.ui.model.TBU_UI;
import net.optix.ui.ucc.IUIUCC;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.honglian.license.decode.DecodeHandle;

/*******************************************************************************
 * 默认登录
 * 
 * @author 姜志光
 * 
 ******************************************************************************/
@ParentPackage(value = "basic")
@Namespace(value = "/eduPlan")
@Action(value = "login", results = {
		@Result(name = "success", type = "redirect", location = "index.action"),
		@Result(name = "loginInput", type = "theme", location = "/resource/appPlan/login.html") })
public class LoginAction extends UserLoginAction implements ILoginAction {

	private String uiTitle;

	@SuppressWarnings("deprecation")
	public String execute() {

		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {

			HttpServletRequest request = ServletActionContext.getRequest();
			FileInputStream inputStream = new FileInputStream(
					request.getRealPath("/license.xml"));
			while (true) {
				byte[] datas = new byte[1024];
				int length = inputStream.read(datas);
				if (-1 == length) {
					break;
				}
				outputStream.write(datas, 0, length);
			}
			outputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String, String> infos = new DecodeHandle(outputStream.toByteArray())
				.decode();

		{
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Date endDate = null;
			try {
				endDate = dateFormat.parse(infos.get("endDate"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			// Date today = NetTime.getTime();
			Date today = new Date();
			if (today.compareTo(endDate) > 0) {
				setResult(new StringBuffer("产品已逾期"));
				return refresh;
			}
		}
		System.err.println(infos);
		IUIUCC uiUCC = AppContext.getService("uiUCC", IUIUCC.class);
		TBU_UI ui = uiUCC.findDefaultUI();
		if (null != ui) {
			uiTitle = ui.getUi_apptitle();
		}
		return "loginInput";
	}

	public String doExecute() {

		String result = super.doExecute();
		if (success.equals(result)) {

		}
		return result;
	}

	public String getUiTitle() {
		return uiTitle;
	}

	public void setUiTitle(String uiTitle) {
		this.uiTitle = uiTitle;
	}

}